package com.supermap.search.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.supermap.greendao.XunJianOneTimeDao;
import com.supermap.search.R;
import com.supermap.search.Url_name;
import com.supermap.search.javabean.XunJianOneTime;
import com.supermap.search.sys.loc.LocationApplication;
import com.supermap.search.utils.NetUtil;
import com.supermap.search.utils.SpUtils;
import com.yolanda.nohttp.Binary;
import com.yolanda.nohttp.FileBinary;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;
import com.yolanda.nohttp.rest.StringRequest;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wyw on 2017/3/8.
 * 未上传巡检问题详情界面
 */

public class WenTiXiangQingActivity extends AppCompatActivity {

    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_chengdu)
    TextView tvChengdu;
    @BindView(R.id.tv_wentiqingkuang)
    TextView tvWentiqingkuang;
    @BindView(R.id.tv_repairDesc)
    TextView tvRepairDesc;
    @BindView(R.id.tv_repairTime)
    TextView tvRepairTime;
    @BindView(R.id.tv_verifyDesc)
    TextView tvVerifyDesc;
    @BindView(R.id.tv_verifyTime)
    TextView tvVerifyTime;
    @BindView(R.id.ll_photo)
    LinearLayout llPhoto;
    @BindView(R.id.bt_shangchuan)
    Button btShangchuan;
    @BindView(R.id.ll_pb_shangbao)
    LinearLayout llPbShangbao;
    private XunJianOneTime xunJianOneTime;
    private XunJianOneTimeDao dao;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wentixiangqing_weishangchuan);
        ButterKnife.bind(this);
//        初始化展示数据
        initdata();
//巡检记录数据库
        dao = LocationApplication.getInstance().getDaoSession().getXunJianOneTimeDao();
        requestQueue = NoHttp.newRequestQueue();
    }

    private void initdata() {
        Intent intent = this.getIntent();
        xunJianOneTime = (XunJianOneTime) intent.getSerializableExtra("user");
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss");
        tvTime.setText(xunJianOneTime.getReportTime());
        tvRepairTime.setText(xunJianOneTime.getRepairTime());
        tvVerifyTime.setText(xunJianOneTime.getVerifyTime());
        tvVerifyDesc.setText(xunJianOneTime.getVerifyDesc());
        tvRepairDesc.setText(xunJianOneTime.getRepairDesc());
        tvChengdu.setText(xunJianOneTime.getYanzhongchengdu());
        tvWentiqingkuang.setText(xunJianOneTime.getWentiqingkuang());
        List<String> photo = SpUtils.stringToList(xunJianOneTime.getPhotopath());
        //有图片时候动态加载图片
        for (int i = 0; i < photo.size(); i++) {
            ImageView imageView = new ImageView(WenTiXiangQingActivity.this);
            llPhoto.addView(imageView, 800, 1000);
            Glide.with(WenTiXiangQingActivity.this)
                    .load(photo.get(i))
                    .placeholder(R.mipmap.image_loading) //设置占位图
                    .thumbnail(0.1f)
                    .error(R.mipmap.image_loading_error)//加载失败时显示的图片
                    .into(imageView);
        }
    }

    @OnClick(R.id.bt_shangchuan)
    public void onClick() {
        shangchuan();
    }

    private void shangchuan() {
//        这里是上传文件的代码
        {
            llPbShangbao.setVisibility(View.VISIBLE);
//        先判定有沒有网络
            if (NetUtil.isNetworkConnected(WenTiXiangQingActivity.this)) {
//             有网络，上传
                stringRequest = new StringRequest(Url_name.POST_XUNJIAN, RequestMethod.POST);
                stringRequest.add("CENTERX", xunJianOneTime.getX());
                stringRequest.add("CENTERY", xunJianOneTime.getY());
                stringRequest.add("PROBLEMDESC", xunJianOneTime.getWentiqingkuang());
                stringRequest.add("SEVERITYLEVEL", xunJianOneTime.getYanzhongchengdu());
                stringRequest.add("REPORTUSERID", getSharedPreferences(Url_name.SP, 0).getString("name", ""));
                List<String> photo = SpUtils.stringToList(xunJianOneTime.getPhotopath());
                List<Binary> fileList = new ArrayList<>();
                fileList.clear();
                for (int i = 0; i < photo.size(); i++) {
                    fileList.add(new FileBinary(new File(photo.get(i))));
                }
                if (fileList.size() > 0) {
//                当有图片的时候
                    stringRequest.add("image", fileList);
                }
                requestQueue.add(1, stringRequest, new SimpleResponseListener<String>() {
                    @Override
                    public void onSucceed(int what, Response<String> response) {
                        if (response.get().toString().contains("success")) {
                            dao.deleteByKey(xunJianOneTime.getId());
                            llPbShangbao.setVisibility(View.GONE);
                            Toast.makeText(WenTiXiangQingActivity.this, "上传成功！", Toast.LENGTH_SHORT).show();
                            WenTiXiangQingActivity.this.finish();
                        } else {
                            llPbShangbao.setVisibility(View.GONE);
                            Toast.makeText(WenTiXiangQingActivity.this, "上传失败，已保存本地", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                        llPbShangbao.setVisibility(View.GONE);
                        Toast.makeText(WenTiXiangQingActivity.this, "上传失败，已保存本地", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                llPbShangbao.setVisibility(View.GONE);
                Toast.makeText(this, "无网络，已保存本地", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
